<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Normalization and Localization - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.locale.parsing.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.locale.parsing.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.locale.functions.html">Using Zend_Locale</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.locale.html">Zend_Locale</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.locale.date.datesandtimes.html">Working with Dates and Times</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.locale.parsing" class="section"><div class="info"><h1 class="title">Normalization and Localization</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Locale_Format</span> is an internal component used by
        <span class="classname">Zend_Locale</span>. All locale aware classes use
        <span class="classname">Zend_Locale_Format</span> for normalization and localization of numbers and
        dates. Normalization involves parsing input from a variety of data representations, like
        dates, into a standardized, structured representation, such as a <acronym class="acronym">PHP</acronym>
        array with year, month, and day elements.
    </p>

    <p class="para">
        The exact same string containing a number or a date might mean different things to people
        with different customs and conventions. Disambiguation of numbers and dates requires rules
        about how to interpret these strings and normalize the values into a standardized data
        structure. Thus, all methods in <span class="classname">Zend_Locale_Format</span> require a locale
        in order to parse the input data.

        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Default &quot;root&quot; Locale</b><br /></span>
            

            <p class="para">
                If no locale is specified, then normalization and localization will use the standard
                &quot;root&quot; locale, which might yield unexpected behavior, if the input originated in a
                different locale, or output for a specific locale was expected.
            </p>
        </p></blockquote>
    </p>

    <div class="section" id="zend.locale.number.normalize"><div class="info"><h1 class="title">Number normalization: getNumber($input, Array $options)</h1></div>
        

        <p class="para">
            There are many <a href="http://en.wikipedia.org/wiki/Numeral" class="link external">&raquo; number systems</a>
            different from the common <a href="http://en.wikipedia.org/wiki/Decimal" class="link external">&raquo; decimal system</a> (e.g. &quot;3.14&quot;).
            Numbers can be normalized with the  <span class="methodname">getNumber()</span> function to
            obtain the standard decimal representation. for all number-related discussions in this
            manual, <a href="http://en.wikipedia.org/wiki/Arabic_numerals" class="link external">&raquo; Arabic/European numerals
                (0,1,2,3,4,5,6,7,8,9)</a> are implied, unless explicitly stated otherwise. The
            options array may contain a &#039;locale&#039; to define grouping and decimal characters. The
            array may also have a &#039;precision&#039; to truncate excess digits from the result.
        </p>

        <div class="example" id="zend.locale.number.normalize.example-1"><div class="info"><p><b>Example #1 Number normalization</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::getNumber(&#039;13.524,678&#039;,
                                        array(&#039;locale&#039; =&gt; $locale,
                                              &#039;precision&#039; =&gt; 3)
                                       );

print $number; // will return 13524.678
</pre>

        </div>

        <div class="section" id="zend.locale.number.normalize.precision"><div class="info"><h1 class="title">Precision and Calculations</h1></div>
            

            <p class="para">
                Since  <span class="methodname">getNumber($value, array $options = array())</span> can
                normalize extremely large numbers, check the result carefully before using finite
                precision calculations, such as ordinary <acronym class="acronym">PHP</acronym> math operations. For
                example, <strong class="command">if ((string)int_val($number) != $number) {</strong> use <a href="http://www.php.net/bc" class="link external">&raquo; BCMath</a> or <a href="http://www.php.net/gmp" class="link external">&raquo; GMP</a>. Most <acronym class="acronym">PHP</acronym>
                installations support the BCMath extension.
            </p>

            <p class="para">
                Also, the precision of the resulting decimal representation can be rounded to a
                desired length with  <span class="methodname">getNumber()</span> with the option
                &#039;<span class="property">precision</span>&#039;. If no precision is given, no rounding occurs. Use
                only <acronym class="acronym">PHP</acronym> integers to specify the precision.
            </p>

            <p class="para">
                If the resulting decimal representation should be truncated to a desired length
                instead of rounded the option &#039;<span class="property">number_format</span>&#039; can be used
                instead. Define the length of the decimal representation with the desired length
                of zeros. The result will then not be rounded. So if the defined precision within
                <span class="property">number_format</span> is zero the value &quot;1.6&quot; will return &quot;1&quot;, not &quot;2.
                See the example nearby:
            </p>

            <div class="example" id="zend.locale.number.normalize.precision.example-1"><div class="info"><p><b>Example #2 Number normalization with precision</b></p></div>
                

                <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::getNumber(&#039;13.524,678&#039;,
                                        array(&#039;precision&#039; =&gt; 1,
                                              &#039;locale&#039; =&gt; $locale)
                                       );
print $number; // will return 13524.7

$number = Zend_Locale_Format::getNumber(&#039;13.524,678&#039;,
                                        array(&#039;number_format&#039; =&gt; &#039;#.00&#039;,
                                              &#039;locale&#039; =&gt; $locale)
                                       );
print $number; // will return 13524.67
</pre>

            </div>
        </div>
    </div>

    <div class="section" id="zend.locale.number.localize"><div class="info"><h1 class="title">Number localization</h1></div>
        

        <p class="para">
             <span class="methodname">toNumber($value, array $options = array())</span> can localize numbers
            to the following <a href="zend.locale.appendix.html" class="link">supported locales</a>. This
            function will return a localized string of the given number in a conventional format for
            a specific locale. The &#039;number_format&#039; option explicitly specifies a non-default number
            format for use with  <span class="methodname">toNumber()</span>.
        </p>

        <div class="example" id="zend.locale.number.localize.example-1"><div class="info"><p><b>Example #3 Number localization</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::toNumber(13547.36,
                                       array(&#039;locale&#039; =&gt; $locale));

// will return 13.547,36
print $number;
</pre>

        </div>

        <p class="para">
            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Unlimited length</b><br /></span>
                

                <p class="para">
                     <span class="methodname">toNumber()</span> can localize numbers with unlimited length.
                    It is not related to integer or float limitations.
                </p>
            </p></blockquote>
        </p>

        <p class="para">
            The same way as within  <span class="methodname">getNumber()</span>,
             <span class="methodname">toNumber()</span> handles precision. If no precision is given, the
            complete localized number will be returned.
        </p>

        <div class="example" id="zend.locale.number.localize.example-2"><div class="info"><p><b>Example #4 Number localization with precision</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::toNumber(13547.3678,
                                       array(&#039;precision&#039; =&gt; 2,
                                             &#039;locale&#039; =&gt; $locale));

// will return 13.547,37
print $number;
</pre>

        </div>

        <p class="para">
            Using the option &#039;number_format&#039; a self defined format for generating a number can be
            defined. The format itself has to be given in <acronym class="acronym">CLDR</acronym> format as
            described below. The locale is used to get separation, precision and other number
            formatting signs from it. German for example defines &#039;,&#039; as precision separation and in
            English the &#039;.&#039; sign is used.
        </p>

        <table id="zend.locale.number.localize.table-1" class="doctable table"><div class="info"><caption><b>Format tokens for self generated number formats</b></caption></div>
            

            
                <thead valign="middle">
                    <tr valign="middle">
                        <th>Token</th>
                        <th>Description</th>
                        <th>Example format</th>
                        <th>Generated output</th>
                    </tr>

                </thead>


                <tbody valign="middle" class="tbody">
                    <tr valign="middle">
                        <td align="left">#0</td>
                        <td align="left">Generates a number without precision and separation</td>
                        <td align="left">#0</td>
                        <td align="left">1234567</td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">,</td>

                        <td align="left">
                            Generates a separation with the length from separation to next
                            separation or to 0
                        </td>

                        <td align="left">#,##0</td>
                        <td align="left">1,234,567</td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">#,##,##0</td>

                        <td align="left">
                            Generates a standard separation of 3 and all following separations with
                            2
                        </td>

                        <td align="left">#,##,##0</td>
                        <td align="left">12,34,567</td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">.</td>
                        <td align="left">Generates a precision</td>
                        <td align="left">#0.#</td>
                        <td align="left">1234567.1234</td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">0</td>
                        <td align="left">Generates a precision with a defined length</td>
                        <td align="left">#0.00</td>
                        <td align="left">1234567.12</td>
                    </tr>

                </tbody>
            
        </table>


        <div class="example" id="zend.locale.number.localize.example-3"><div class="info"><p><b>Example #5 Using a self defined number format</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::toNumber(13547.3678,
                                       array(&#039;number_format&#039; =&gt; &#039;#,#0.00&#039;,
                                             &#039;locale&#039; =&gt; &#039;de&#039;)
                                      );

// will return 1.35.47,36
print $number;

$number = Zend_Locale_Format::toNumber(13547.3,
                                       array(&#039;number_format&#039; =&gt; &#039;#,##0.00&#039;,
                                             &#039;locale&#039; =&gt; &#039;de&#039;)
                                       );

// will return 13.547,30
print $number;
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.number.test"><div class="info"><h1 class="title">Number testing</h1></div>
        

        <p class="para">
             <span class="methodname">isNumber($value, array $options = array())</span> checks if a given
            string is a number and returns <b><tt>TRUE</tt></b> or <b><tt>FALSE</tt></b>.
        </p>

        <div class="example" id="zend.locale.number.test.example-1"><div class="info"><p><b>Example #6 Number testing</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale();
if (Zend_Locale_Format::isNumber(&#039;13.445,36&#039;, array(&#039;locale&#039; =&gt; &#039;de_AT&#039;))) {
    print &quot;Number&quot;;
} else {
    print &quot;not a Number&quot;;
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.float.normalize"><div class="info"><h1 class="title">Float value normalization</h1></div>
        

        <p class="para">
            Floating point values can be parsed with the
             <span class="methodname">getFloat($value, array $options = array())</span> function. A floating
            point value will be returned.
        </p>

        <div class="example" id="zend.locale.float.normalize.example-1"><div class="info"><p><b>Example #7 Floating point value normalization</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::getFloat(&#039;13.524,678&#039;,
                                       array(&#039;precision&#039; =&gt; 2,
                                             &#039;locale&#039; =&gt; $locale)
                                      );

// will return 13524.68
print $number;
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.float.localize"><div class="info"><h1 class="title">Floating point value localization</h1></div>
        

        <p class="para">
             <span class="methodname">toFloat()</span> can localize floating point values. This function
            will return a localized string of the given number.
        </p>

        <div class="example" id="zend.locale.float.localize.example-1"><div class="info"><p><b>Example #8 Floating point value localization</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::toFloat(13547.3655,
                                      array(&#039;precision&#039; =&gt; 1,
                                            &#039;locale&#039; =&gt; $locale)
                                     );

// will return 13.547,4
print $number;
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.float.test"><div class="info"><h1 class="title">Floating point value testing</h1></div>
        

        <p class="para">
             <span class="methodname">isFloat($value, array $options = array())</span> checks if a given
            string is a floating point value and returns <b><tt>TRUE</tt></b> or
            <b><tt>FALSE</tt></b>.
        </p>

        <div class="example" id="zend.locale.float.test.example-1"><div class="info"><p><b>Example #9 Floating point value testing</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
if (Zend_Locale_Format::isFloat(&#039;13.445,36&#039;, array(&#039;locale&#039; =&gt; $locale))) {
    print &quot;float&quot;;
} else {
    print &quot;not a float&quot;;
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.integer.normalize"><div class="info"><h1 class="title">Integer value normalization</h1></div>
        

        <p class="para">
            Integer values can be parsed with the  <span class="methodname">getInteger()</span> function. A
            integer value will be returned.
        </p>

        <div class="example" id="zend.locale.integer.normalize.example-1"><div class="info"><p><b>Example #10 Integer value normalization</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::getInteger(&#039;13.524,678&#039;,
                                         array(&#039;locale&#039; =&gt; $locale));

// will return 13524
print $number;
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.integer.localize"><div class="info"><h1 class="title">Integer point value localization</h1></div>
        

        <p class="para">
             <span class="methodname">toInteger($value, array $options = array())</span> can localize
            integer values. This function will return a localized string of the given number.
        </p>

        <div class="example" id="zend.locale.integer.localize.example-1"><div class="info"><p><b>Example #11 Integer value localization</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$number = Zend_Locale_Format::toInteger(13547.3655,
                                        array(&#039;locale&#039; =&gt; $locale));

// will return 13.547
print $number;
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.integer.test"><div class="info"><h1 class="title">Integer value testing</h1></div>
        

        <p class="para">
             <span class="methodname">isInteger($value, array $options = array())</span> checks if a given
            string is an integer value and returns <b><tt>TRUE</tt></b> or
            <b><tt>FALSE</tt></b>.
        </p>

        <div class="example" id="zend.locale.integer.test.example-1"><div class="info"><p><b>Example #12 Integer value testing</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
if (Zend_Locale_Format::isInteger(&#039;13.445&#039;, array(&#039;locale&#039; =&gt; $locale))) {
    print &quot;integer&quot;;
} else {
    print &quot;not an integer&quot;;
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.locale.numbersystems"><div class="info"><h1 class="title">Numeral System Conversion</h1></div>
        

        <p class="para">
             <span class="methodname">Zend_Locale_Format::convertNumerals()</span> converts digits between
            different <a href="http://en.wikipedia.org/wiki/Arabic_numerals" class="link external">&raquo; numeral
                systems</a>, including the standard Arabic/European/Latin numeral system
            (0,1,2,3,4,5,6,7,8,9), not to be confused with <a href="http://en.wikipedia.org/wiki/Eastern_Arabic_numerals" class="link external">&raquo; Eastern Arabic
                numerals</a> sometimes used with the Arabic language to express numerals.
            Attempts to use an unsupported numeral system will result in an exception, to avoid
            accidentally performing an incorrect conversion due to a spelling error. All characters
            in the input, which are not numerals for the selected numeral system, are copied to the
            output with no conversion provided for unit separator characters.
            <span class="classname">Zend_Locale</span>* components rely on the data provided by
            <acronym class="acronym">CLDR</acronym> (see their <a href="http://unicode.org/cldr/data/diff/supplemental/languages_and_scripts.html?sortby=date" class="link external">&raquo; list
                of scripts grouped by language</a>).
        </p>

        <p class="para">
            In <acronym class="acronym">CLDR</acronym> and hereafter, the Europena/Latin numerals will
            be referred to as &quot;Latin&quot; or by the assigned 4-letter code &quot;Latn&quot;.
            Also, the <acronym class="acronym">CLDR</acronym> refers to this numeral systems as &quot;scripts&quot;.
        </p>

        <p class="para">
            Suppose a web form collected a numeric input expressed using Eastern Arabic digits
            &quot;١‎٠٠&quot;. Most software and <acronym class="acronym">PHP</acronym> functions expect input using Arabic
            numerals. Fortunately, converting this input to its equivalent Latin numerals &quot;100&quot;
            requires little effort using  <span class="methodname">convertNumerals($inputNumeralString,
                $sourceNumeralSystem, $destNumeralSystem)</span>, which returns the
            <var class="varname">$input</var> with numerals in the script
            <var class="varname">$sourceNumeralSystem</var> converted to the script
            <var class="varname">$destNumeralSystem</var>.
        </p>

        <div class="example" id="zend.locale.numbersystems.example-1"><div class="info"><p><b>Example #13 Converting numerals from Eastern Arabic scripts to European/Latin scripts</b></p></div>
            

            <pre class="programlisting brush: php">
$arabicScript = &quot;١‎٠٠&quot;;   // Arabic for &quot;100&quot; (one hundred)
$latinScript = Zend_Locale_Format::convertNumerals($arabicScript,
                                                   &#039;Arab&#039;,
                                                   &#039;Latn&#039;);

print &quot;\nOriginal:   &quot; . $arabicScript;
print &quot;\nNormalized: &quot; . $latinScript;
</pre>

        </div>

        <p class="para">
            Similarly, any of the supported numeral systems may be converted to any other supported
            numeral system.
        </p>

        <div class="example" id="zend.locale.numbersystems.example-2"><div class="info"><p><b>Example #14 Converting numerals from Latin script to Eastern Arabic script</b></p></div>
            

            <pre class="programlisting brush: php">
$latinScript = &#039;123&#039;;
$arabicScript = Zend_Locale_Format::convertNumerals($latinScript,
                                                    &#039;Latn&#039;,
                                                    &#039;Arab&#039;);

print &quot;\nOriginal:  &quot; . $latinScript;
print &quot;\nLocalized: &quot; . $arabicScript;
</pre>

        </div>

        <div class="example" id="zend.locale.numbersystems.example-3"><div class="info"><p><b>Example #15 
                Getting 4 letter CLDR script code using a native-language name of the script
            </b></p></div>
            

            <pre class="programlisting brush: php">
function getScriptCode($scriptName, $locale)
{
    $scripts2names = Zend_Locale_Data::getList($locale, &#039;script&#039;);
    $names2scripts = array_flip($scripts2names);
    return $names2scripts[$scriptName];
}
echo getScriptCode(&#039;Latin&#039;, &#039;en&#039;); // outputs &quot;Latn&quot;
echo getScriptCode(&#039;Tamil&#039;, &#039;en&#039;); // outputs &quot;Taml&quot;
echo getScriptCode(&#039;tamoul&#039;, &#039;fr&#039;); // outputs &quot;Taml&quot;
</pre>

        </div>

        <p class="para">
            For a list of supported numeral systems call
             <span class="methodname">Zend_Locale::getTranslationList(&#039;numberingsystem&#039;, &#039;en&#039;)</span>.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.locale.functions.html">Using Zend_Locale</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.locale.html">Zend_Locale</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.locale.date.datesandtimes.html">Working with Dates and Times</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.locale.html">Zend_Locale</a></li>
  <li><a href="zend.locale.introduction.html">Introduction</a></li>
  <li><a href="zend.locale.functions.html">Using Zend_Locale</a></li>
  <li class="active"><a href="zend.locale.parsing.html">Normalization and Localization</a></li>
  <li><a href="zend.locale.date.datesandtimes.html">Working with Dates and Times</a></li>
  <li><a href="zend.locale.appendix.html">Supported locales</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>